<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <PointerText/>
            </n-icon>
            <Handle id="KeyWordsExtraction-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="KeyWordsExtraction-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>关键词提取</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="关键词提取">
                <header>
                    <p>
                        <span>提取关键词个数: &nbsp;</span>
                        <n-input-number placeholder="" v-model:value="number"/>
                    </p>
                    <!--                    <p>-->
                    <!--                        <n-button>提取关键词</n-button>-->
                    <!--                    </p>-->
                </header>
                <div class="dict">
                    关键词权重表:
                    <div>
                        <n-data-table
                            :columns="props.data.data.title"
                            :data="props.data.data.data"
                            :pagination="pagination"
                            :row-props="rowProps"
                            style="height: 300px"
                            flex-height
                        />
                    </div>
                </div>
                <div>
                    移除词:
                    <n-input
                        type="textarea"
                        :disabled="true"
                        v-model:value="remove_word_computed"
                        placeholder="点击上方关键词添加"
                    />
                </div>
                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="implementation">提取关键词</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {PointerText} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()

// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})
const number = ref(20)
const remove_word = ref([])
const remove_word_computed = computed(() => {
    let text = ''
    for (let i of remove_word.value) {
        text = text + i + ', '
    }
    return text
})

function rowProps(row) {
    return {
        style: "cursor: pointer;",
        onClick: () => {
            clickWord(row.word)
        }
    };
}

function clickWord(word) {
    let item = remove_word.value.indexOf(word)
    if (item === -1) {
        // 没有则添加
        addRemoveWord(word)
    } else {
        // 点击的单词在删除列表中 移除
        removedRemoveWord(item)
    }

}

function addRemoveWord(word) {
    remove_word.value.push(word)
}

function removedRemoveWord(item) {
    remove_word.value.splice(item, 1)
}

const implementation = () => {
    workApi.post("perform_tasks/keyword_analysis", {
        number: number.value,
        remove_word: remove_word.value,
        data: props.data.text
    }).then(r => {
        console.log(r)
        props.data.data = r.data
    })
}


function delWord(number) {
    props.data.data.splice(number, 1)
}

const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.text = j.data.text
                    return
                }
            }
        }
    }
}
// 单页数据量
const pagination = {
    pageSize: 10
}

</script>

<style scoped lang="less">
header {
    display: flex;
    justify-content: space-between;

    p {
        display: flex;

        span {
            display: flex;
            align-items: center
        }

        .n-input-number {
            width: 100px;
        }
    }
}


.dict {
    > div {
        width: 500px;
    }
}
</style>